package com.ruoyi.sfqy.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.sfqy.domain.SfqyFj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.sfqy.domain.Sfqy;
import com.ruoyi.sfqy.service.ISfqyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 示范企业Controller
 *
 * @author huacai
 * @date 2024-10-26
 */
@RestController
@RequestMapping("/sfqy/sfqy")
public class SfqyController extends BaseController {
    @Resource
    private ISfqyService sfqyService;

    /**
     * 查询示范企业列表
     */
    @PreAuthorize("@ss.hasPermi('sfqy:sfqy:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sfqy sfqy) {
        startPage();
        List<Sfqy> list = sfqyService.selectSfqyList(sfqy);
        return getDataTable(list);
    }

    /**
     * 导出示范企业列表
     */
    @PreAuthorize("@ss.hasPermi('sfqy:sfqy:export')")
    @Log(title = "示范企业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sfqy sfqy) {
        List<Sfqy> list = sfqyService.selectSfqyList(sfqy);
        ExcelUtil<Sfqy> util = new ExcelUtil<Sfqy>(Sfqy.class);
        util.exportExcel(response, list, "示范企业数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Sfqy> util = new ExcelUtil<Sfqy>(Sfqy.class);
        util.importTemplateExcel(response, "示范企业数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "示范企业", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sfqy:sfqy:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Sfqy> util = new ExcelUtil<Sfqy>(Sfqy.class);
        InputStream inputStream = file.getInputStream();
        List<Sfqy> list = util.importExcel(inputStream);
        inputStream.close();
        int count = sfqyService.batchInsertSfqy(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取示范企业详细信息
     */
    @PreAuthorize("@ss.hasPermi('sfqy:sfqy:query')")
    @GetMapping(value = "/{sfqyId}")
    public AjaxResult getInfo(@PathVariable("sfqyId") String sfqyId) {
        return success(sfqyService.selectSfqyBySfqyId(sfqyId));
    }

    /**
     * 新增示范企业
     */
    @PreAuthorize("@ss.hasPermi('sfqy:sfqy:add')")
    @Log(title = "示范企业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sfqy sfqy) {
        sfqy.setUserId(Math.toIntExact(getUserId())); //用户ID
        sfqy.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        sfqy.setCreateBy(getUsername()); //创建人
        return toAjax(sfqyService.insertSfqy(sfqy));
    }

    /**
     * 修改示范企业
     */
    @PreAuthorize("@ss.hasPermi('sfqy:sfqy:edit')")
    @Log(title = "示范企业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sfqy sfqy) {
        return toAjax(sfqyService.updateSfqy(sfqy));
    }

    /**
     * 删除示范企业
     */
    @PreAuthorize("@ss.hasPermi('sfqy:sfqy:remove')")
    @Log(title = "示范企业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sfqyIds}")
    public AjaxResult remove(@PathVariable String[] sfqyIds) {
        return toAjax(sfqyService.deleteSfqyBySfqyIds(sfqyIds));
    }

    /**
     * 获得示范企业主键ID (UUID)
     */
    @GetMapping("/getSfQyId")
    public AjaxResult getSfQyId() {
        String getSfQyId = IdUtils.fastSimpleUUID();
        return success(getSfQyId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertSfqyFjBySfqyId(@RequestBody SfqyFj sfqyFj) {
        return toAjax(sfqyService.insertSfqyFjBySfqyId(sfqyFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectSfqyFjList(String sId) {
        SfqyFj sfqyFj = new SfqyFj();
        sfqyFj.setSfqyId(sId);
        return AjaxResult.success(sfqyService.selectSfqyFjList(sfqyFj));
    }

    /**
     * 删除示范企业附件
     */
    @Log(title = "示范企业附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult delSfQyFj(@PathVariable Integer[] fjIds) {
        return toAjax(sfqyService.deleteSfqyFjByFjIds(fjIds));
    }

}
